package cn.abcsys.cloud.devops.pipeline.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.pipeline.common.GitLab;
import cn.abcsys.cloud.devops.pipeline.common.GridBean;
import cn.abcsys.cloud.devops.pipeline.core.PipelineCore;
import cn.abcsys.cloud.devops.pipeline.model.TaskModel;
import cn.abcsys.cloud.devops.pipeline.service.PipelineService;
import cn.abcsys.cloud.devops.pipeline.service.TaskEventService;
import cn.abcsys.cloud.devops.pipeline.util.Result;
@RestController
@RequestMapping("/pipeline")
public class PipelineController {
	private static final Logger LOGGER = Logger.getLogger(PipelineController.class);
	@Autowired
	private PipelineCore pipelineCore;
	@Autowired
	private PipelineService pipelineService;
	@Autowired
	private TaskEventService taskEventService;
	@RequestMapping(value = "/applications", method = { RequestMethod.GET })
	@ResponseBody
	public GridBean clusterList(@RequestParam Map<String,Object> params,HttpServletRequest request){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			TaskModel taskModel = new TaskModel();
			net.sf.json.JSONObject orig = net.sf.json.JSONObject.fromObject(params);
			taskModel = (TaskModel) net.sf.json.JSONObject.toBean(orig,TaskModel.class);
			HttpSession httpSession = request.getSession();
			httpSession.setAttribute("userId", taskModel.getUserId());
			httpSession.setAttribute("taskId", taskModel.getTaskId());
			return pipelineService.getApplications(taskModel);
		}
	}
	@RequestMapping(value = "/projects", method = { RequestMethod.GET })
	@ResponseBody
	public String getGitProjects(@RequestParam(value = "protocol", required = true) String http_protocol,
			@RequestParam(value = "hostname", required = true) String gitLab_url,
			@RequestParam(value = "port", required = true) String gitLab_port,
			@RequestParam(value = "token", required = true) String private_token){
		GitLab gitLab = new GitLab();
		gitLab.setGitLab_url(gitLab_url);
		gitLab.setGitLab_port(gitLab_port);
		gitLab.setHttp_protocol(http_protocol);
		Result result = pipelineCore.getGitProjects(gitLab, private_token);
		if(result.isSuccess()){
			return result.getData().toString();
		}else{
			JSONObject object = new JSONObject();
			object.put("error", result.getMessage());
			return object.toJSONString();
		}
		
	}
	@RequestMapping(value = "/createTask", method = { RequestMethod.GET})
	@ResponseBody
	public Result createTask(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.createTask(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/updateTask", method = { RequestMethod.GET})
	@ResponseBody
	public Result updateTask(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.updateTask(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/startTask", method = { RequestMethod.GET})
	@ResponseBody
	public Result startTask(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.startTask(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	/**
	 * 
	* @Title: getTask 
	* @Description: 获取任务详情 
	* @param @param taskId
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/getTask", method = { RequestMethod.GET})
	@ResponseBody
	public Result getTask(Integer taskId){
		if(null==taskId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return pipelineService.getTask(taskId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	/**
	 * 
	* @Title: getTaskByProjectId 
	* @Description: 根据项目获取相关任务
	* @param @param projectId
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/getTaskByProjectId", method = { RequestMethod.GET})
	@ResponseBody
	public Result getTaskByProjectId(@RequestParam Integer projectId){
		if(null==projectId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return pipelineService.getTaskByProjectId(projectId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/getTaskEvent", method = { RequestMethod.GET})
	@ResponseBody
	public Result getTaskEvent(@RequestParam Integer taskId){
		if(null==taskId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return taskEventService.getTaskEvents(taskId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/deleteTask", method = { RequestMethod.GET})
	@ResponseBody
	public Result deleteTask(@RequestParam Integer taskId){
		if(null==taskId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return pipelineService.deleteTask(taskId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/deleteProject", method = { RequestMethod.GET})
	@ResponseBody
	public Result deleteProject(@RequestParam Integer projectId){
		if(null==projectId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return pipelineService.deleteProject(projectId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/buildJob", method = { RequestMethod.GET})
	@ResponseBody
	public Result jenkinsJobBuild(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.jenkinsJobBuild(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/buildJobConsoleOutput", method = { RequestMethod.GET})
	@ResponseBody
	public Result buildJobConsoleOutput(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.jenkinsBuildConsoleOutput(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	/**
	 * 
	* @Title: clairLogs 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param params{imageUuid}
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/clairLogs", method = { RequestMethod.GET})
	@ResponseBody
	public Result clairLogs(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.clairLogs(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	/**
	* @Title: clairScanner 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param params{imageUuid}
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/clairScanner", method = { RequestMethod.GET})
	@ResponseBody
	public Result clairScanner(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.clairScanner(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	/**
	* @Title: makeImage 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param params{targetFolder,imageName,Dockerfile}
	* @param @return    设定文件 
	* @return Result    返回类型 
	* @throws
	 */
	@RequestMapping(value = "/makeImage", method = { RequestMethod.GET})
	@ResponseBody
	public Result makeImage(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				JSONObject paramIn = JSONObject.parseObject(JSON.toJSONString(params));
				return pipelineService.makeImage(paramIn);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/getEventById", method = { RequestMethod.GET})
	@ResponseBody
	public Result getEventById(Integer eventId){
		if(null==eventId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return taskEventService.getTaskEventDetail(eventId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
	@RequestMapping(value = "/eventLogOutput", method = { RequestMethod.GET})
	@ResponseBody
	public Result eventLogOutput(Integer eventId){
		if(null==eventId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			try{
				return taskEventService.eventLogOutput(eventId);
			}catch(Exception e){
				LOGGER.error("执行异常："+e.getMessage());
				return new Result(false, "执行异常："+e.getMessage());
			}
		}
	}
}
